
<template>
  <div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
            v-model="editUser.currentValue"
            :name="editUser.editKey"
            :label="editUser.editName"
            :placeholder="'请输入您的' + editUser.editName"
        />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>

  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import {useRoute} from "vue-router";
import request from "../../plugins/request.ts";
import router from "../../router";

const route = useRoute();
const editUser = ref({
  id: route.query.id,
  editKey: route.query.editKey,
  currentValue: route.query.currentValue,
  editName: route.query.editName
})

const onSubmit = async () => {
  // 只有值发生改变才能向后台发起请求
  if (editUser.value.currentValue !== route.query.currentValue) {
    const result = await request.post("/user/update", {
      "id": editUser.value.id,
      [editUser.value.editKey as any]: editUser.value.currentValue
    })
    if (result.data.data > 0) {
      showSuccessToast("成功")
      router.back()
    }
  } else {
    showFailToast(`${editUser.value.editName}未发生改变`,);
  }
}
</script>

<style scoped>

</style>